import wechatValidate from 'wechat-validate'
Page({
  behaviors: [wechatValidate],
  data: {
    currentDate: +new Date(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    houseInfo: '', // 展示的房屋名称
    repairItemName: '', // 展示报修的项目

    attachment: [], // 附件图片
    houseId: '', // 提交房间id
    repairItemId: '', // 提交报修id
    appointment: '', // 预约日期
    mobile: '', // 手机号
    description: '', // 问题描述
  },
  onShow() {
    this.getHouseList()
    this.getRepairItem()
  },
  // 展示选择房屋动作面板
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  // 关闭选择房屋动作面板
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 获取审核通过的房屋列表
  async getHouseList() {
    const { data: houseList } = await wx.http.get('/house')
    this.setData({ houseList })
  },
  // 选择了要维修的房屋
  selectHouse(ev) {
    // houseId 提交的字段使用houseId houseInfo为了在页面展示选中的房屋的名字
    this.setData({ houseId: ev.detail.id, houseInfo: ev.detail.name })
  },
  // ----------------------------------
  // 展示维修项目动作面板
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  // 关闭维修项目动作面板
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  // 获取维修项目
  async getRepairItem() {
    const { data: repairItem } = await wx.http.get('/repairItem')
    this.setData({ repairItem })
  },
  // 选择维修项目
  selectRepair(ev) {
    console.log(ev.detail)
    // repairItemId提交的字段使用repairItemId repairItemName为了在页面展示选中的报修的名字
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name,
    })
  },
  //--------------------
  // 展示预约日期弹出层
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  // 关闭预约日期弹出层
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 点击确认
  confirm(ev) {
    // console.log(ev.detail)
    this.setData({ appointment: wx.utils.dateFormat(ev.detail) })
    this.closeDateLayer()
  },

  // 附件图片上传
  afterRead(ev) {
    console.log(ev.detail.file.tempFilePath)
    // 拿着路径进行上传
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: ev.detail.file.tempFilePath,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`,
      },
      success: ({ data }) => {
        // 追加数据给attachment
        this.data.attachment.push(JSON.parse(data).data)
        // 设置一次
        this.setData({ attachment: this.data.attachment })
      },
    })
  },
  // 移除附件图片
  del(ev) {
    // console.log(ev.detail.index) // 索引
    // 截取
    this.data.attachment.splice(ev.detail.index, 1)
    this.setData({ attachment: this.data.attachment })
  },

  rules: {
    houseId: [{ required: true, message: '请选择报修的房屋' }],
    repairItemId: [{ required: true, message: '请选择报修的项目' }],
    mobile: [
      { required: true, message: '请输入手机号' },
      { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' },
    ],
    appointment: [{ required: true, message: '请选择预约日期' }],
    description: [{ required: true, message: '请输入描述信息' }],
  },
  // 新增报修
  async goList() {
    // 1. 预校验
    if (!this.validate()) return
    //  2. 发送请求
    const { id, attachment, houseId, repairItemId, appointment, mobile, description } = this.data
    await wx.http.post('/repair', { id, attachment, houseId, repairItemId, appointment, mobile, description })
    // 3. 跳转
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  async onLoad({ id }) {
    console.log(id)
    if (id) {
      const { data } = await wx.http.get(`/repair/${id}`)
      this.setData({ ...data })
    }
  },
})
